<?php

namespace app\common\job;

use app\common\helper\AliOss;
use app\common\helper\WanX;
use app\common\model\QwenOrder;
use app\common\model\User;
use think\Exception;
use think\queue\Job;

class AiOrder
{
    public function create(Job $job, $id)
    {
        $order = \app\common\model\AiOrder::where("id",$id)->find();
        if ($order && !$order->task_id && $job->attempts() < 5) {
            $ai = new WanX();
            if ($order->type == 'v1') {
                $result =  $ai->v1($order->prompt,$order->n,$order->size);
            } elseif ($order->type == 'repaint') {
                $result =  $ai->repaint($order);
            } else {
                $result =  $ai->back($order);
            }
            if (isset($result['output'])) {
                $order->save([
                    "task_id" => $result['output']['task_id'],
                    "task_status" => $result['output']['task_status']
                ]);
                if (in_array($result['output']['task_status'],['PENDING','RUNNING'])) {
                    queue('app\common\job\AiOrder@pull',$order->id,7,'pull');
                }
            } else {
                $order->save([
                    "err_msg" => $result['message'],
                ]);
            }
        }
        $job->delete();
    }

    public function pull(Job $job, $id)
    {
        echo $id . PHP_EOL;
        try {
            $order = \app\common\model\AiOrder::where("id",$id)->find();
            if ($order && $order->task_id) {
                if (in_array($order->task_status,['PENDING','RUNNING'])) {
                    $ai = new WanX();
                    $ai->search($order->task_id);
                }
            }
        } catch (Exception $exception) {
            echo $exception->getMessage();
        }
        $job->delete();
   }

   public function upload(Job $job, $image)
   {
       $oss = new AliOss();
       $path = ltrim($image['path'],'/');
       $oss->multiUploadFile($path,  public_path() . $path, $image['id']);
       $job->delete();
   }

   public function qwen(Job $job, $data)
   {
        $q = new WanX();
        $order = QwenOrder::where("id",$data['id'])->find();
        if ($order) {
            $user = User::where('id',$order->user_id)->find();
            if ($user && $user->status == 1) {
                if ($user->qwen_token > 1000) {
                    $result = $q->qianwen($data['prompt'],$data['model']);
                    if (isset($result['output'])) {
                        $order->total_token = round($result['usage']['total_tokens'] * 1.2);
                        $order->real_token = $result['usage']['total_tokens'];
                        $order->task_status = "SUCCESSED";
                        $order->content = $result['output']['text'];
                        $order->save();
                        $user->qwen_token -=  $order->total_token;
                        $user->save();
                    }
                }
            }
        }
       $job->delete();
   }
}